#include "addfielddialog.h"
#include "ui_addfielddialog.h"

AddFieldDialog::AddFieldDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AddFieldDialog)
{
    ui->setupUi(this);
    iniMainDialog();

    connect(this,&AddFieldDialog::sigAddField,
            AppEvent::getInstance(),&AppEvent::on_AddField);

    connect(btnOK,&QPushButton::clicked,
            this,&AddFieldDialog::on_btnOK_clicked);
    connect(btnCancel,&QPushButton::clicked,
            this,&AddFieldDialog::on_btnCancel_clicked);
}

AddFieldDialog::~AddFieldDialog()
{
    delete ui;
}

void AddFieldDialog::iniMainDialog()
{
    QFormLayout *formLayout=new QFormLayout();
    editFieldName=new QLineEdit();
    comboxFieldType=new QComboBox();
    comboxFieldType->addItem(QObject::tr("int"));
    comboxFieldType->addItem(QObject::tr("double"));
    comboxFieldType->addItem(QObject::tr("text"));
    formLayout->insertRow(0,QObject::tr("Field Name:"),editFieldName);
    formLayout->insertRow(1,QObject::tr("Field Type:"),comboxFieldType);
    btnOK=new QPushButton();
    btnOK->setText(QObject::tr("OK"));
    btnCancel=new QPushButton();
    btnCancel->setText(QObject::tr("Cancel"));
    QHBoxLayout *btnLayout=new QHBoxLayout();
    btnLayout->addWidget(btnOK);
    btnLayout->addWidget(btnCancel);
    QVBoxLayout *mainLayout=new QVBoxLayout();
    mainLayout->addLayout(formLayout);
    mainLayout->addLayout(btnLayout);
    this->setLayout(mainLayout);
}

void AddFieldDialog::on_btnOK_clicked()
{
    QString type=comboxFieldType->currentText();
    GeoFieldType fieldType;
    if(type=="int"){
        fieldType=kFieldInt;
    }
    if(type=="double"){
        fieldType=kFieldDouble;
    }
    if(type=="text"){
        fieldType=kFieldText;
    }
    QString fieldName=editFieldName->text();
    emit sigAddField(fieldName,fieldType);
    this->close();
}
void AddFieldDialog::on_btnCancel_clicked()
{
    this->close();
}
